package com.example.covid19.controller;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.covid19.common.Dto.CkcDto;
import com.example.covid19.common.Dto.CrmDto2;
import com.example.covid19.common.Dto.Crmdto3;
import com.example.covid19.common.Dto.StuAddDto;
import com.example.covid19.common.lang.Result;
import com.example.covid19.entity.College;
import com.example.covid19.entity.SelectClass.CrmDto;
import com.example.covid19.mapper.CollegeMapper;
import com.example.covid19.service.CollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.Console;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author BaiZhengChun
 * @since 2023-03-07
 */
@RestController
@RequestMapping("/college")
public class CollegeController {

    @Autowired
    CollegeMapper collegeMapper;
    @Autowired
    CollegeService collegeService;

    /**
     *
     * @return学生管理界面下拉选择的挂载数据
     */
    @RequestMapping("/getCrm")
    public Result getCrm(){
        List<CrmDto>crmDtoList = collegeMapper.getCrm();
        JSONArray treeData = new JSONArray();
        for (CrmDto crmDto : crmDtoList) {
            JSONObject collegeObj = findObjectByKey(treeData, "label", crmDto.getCname());

            if (collegeObj == null) {
                collegeObj = new JSONObject();
                collegeObj.set("label", crmDto.getCname());
                collegeObj.set("value", crmDto.getCname());
                collegeObj.set("key", crmDto.getCname());
                collegeObj.set("children", new JSONArray());
                treeData.add(collegeObj);
            }

            JSONArray departments = collegeObj.getJSONArray("children");
            JSONObject departmentObj = findObjectByKey(departments, "label", crmDto.getRname());

            if (departmentObj == null) {
                departmentObj = new JSONObject();
                departmentObj.set("label", crmDto.getRname());
                departmentObj.set("value", crmDto.getRname());
                departmentObj.set("key", crmDto.getCname() + "-" + crmDto.getRname());
                departmentObj.set("children", new JSONArray());
                departments.add(departmentObj);
            }

            JSONArray majors = departmentObj.getJSONArray("children");
            JSONObject majorObj = new JSONObject();
            majorObj.set("label", crmDto.getMajeurname());
            majorObj.set("value", crmDto.getMajeurname());
            majorObj.set("key", crmDto.getCname() + "-" + crmDto.getRname() + "-" + crmDto.getMajeurname());
            majors.add(majorObj);
        }

        return Result.success(200, "ok", treeData);
    }
    /**
     *
     * @return添加班级的级联数据
     */
    @RequestMapping("/getCrm2")
    public Result getCrm2(){
        List<Crmdto3>crmDtoList = collegeMapper.getCrm3();
        JSONArray treeData = new JSONArray();
        for (Crmdto3 crmDto : crmDtoList) {
            JSONObject collegeObj = findObjectByKey(treeData, "label", crmDto.getCname());

            if (collegeObj == null) {
                collegeObj = new JSONObject();
                collegeObj.set("label", crmDto.getCname());
                collegeObj.set("value", crmDto.getCid());
                collegeObj.set("key", crmDto.getCname());
                collegeObj.set("children", new JSONArray());
                treeData.add(collegeObj);
            }

            JSONArray departments = collegeObj.getJSONArray("children");
            JSONObject departmentObj = findObjectByKey(departments, "label", crmDto.getRname());

            if (departmentObj == null) {
                departmentObj = new JSONObject();
                departmentObj.set("label", crmDto.getRname());
                departmentObj.set("value", crmDto.getXid());
                departmentObj.set("key", crmDto.getCname() + "-" + crmDto.getRname());
                departmentObj.set("children", new JSONArray());
                departments.add(departmentObj);
            }

            JSONArray majors = departmentObj.getJSONArray("children");
            JSONObject majorObj = new JSONObject();
            majorObj.set("label", crmDto.getMajeurname());
            majorObj.set("value", crmDto.getMid());
            majorObj.set("key", crmDto.getCname() + "-" + crmDto.getRname() + "-" + crmDto.getMajeurname());
            majors.add(majorObj);
        }

        return Result.success(200, "ok", treeData);
    }

    /**
     *
     * @return后台管理下拉框，查询用的
     */
    @RequestMapping("/gettablist")
    public Result gettablist(){
        List<CrmDto>crmDtoList = collegeMapper.getCrm();
        JSONArray treeData = new JSONArray();
        for (CrmDto crmDto : crmDtoList) {
            JSONObject collegeObj = findObjectByKey(treeData, "label", crmDto.getCname());

            if (collegeObj == null) {
                collegeObj = new JSONObject();
                collegeObj.set("label", crmDto.getCname());
                collegeObj.set("value", crmDto.getCname());
                collegeObj.set("key", crmDto.getCname());
                collegeObj.set("children", new JSONArray());
                treeData.add(collegeObj);
            }

            JSONArray departments = collegeObj.getJSONArray("children");
            JSONObject departmentObj = findObjectByKey(departments, "label", crmDto.getRname());

            if (departmentObj == null) {
                departmentObj = new JSONObject();
                departmentObj.set("label", crmDto.getRname());
                departmentObj.set("value", crmDto.getRname());
                departmentObj.set("key", crmDto.getCname() + "-" + crmDto.getRname());
                departments.add(departmentObj);
            }
        }

        return Result.success(200, "ok", treeData);
    }
    /**
     *
     * @return后台管理添加框，这里得value全改成xid
     */
    @RequestMapping("/getidlist")
    public Result getidlist(){
        List<CrmDto2>crmDtoList = collegeMapper.getCrm2();
        JSONArray treeData = new JSONArray();
        for (CrmDto2 crmDto : crmDtoList) {
            JSONObject collegeObj = findObjectByKey(treeData, "label", crmDto.getCname());

            if (collegeObj == null) {
                collegeObj = new JSONObject();
                collegeObj.set("label", crmDto.getCname());
                collegeObj.set("value", crmDto.getCid());
                collegeObj.set("children", new JSONArray());
                treeData.add(collegeObj);
            }

            JSONArray departments = collegeObj.getJSONArray("children");
            JSONObject departmentObj = findObjectByKey(departments, "label", crmDto.getRname());

            if (departmentObj == null) {
                departmentObj = new JSONObject();
                departmentObj.set("label", crmDto.getRname());
                departmentObj.set("value", crmDto.getXid());
                departments.add(departmentObj);
            }
        }

        return Result.success(200, "ok", treeData);
    }

    /**
     *
     * @return前端添加学生班级下拉框内的信息
     */
    @RequestMapping("/getClasses")
    public Result getClasses(){
        JSONArray jsonArray = new JSONArray();
        List<StuAddDto>stringList = collegeMapper.getClasses();
        stringList.forEach(v->{
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("label",v.getClname());
            jsonObject.put("value",v.getCcid());
            jsonObject.put("otherKey",v.getCcid());
            jsonArray.put(jsonObject);
        });
        return Result.success(200,"ok",jsonArray);
    }

    /**
     *
     * @return 学院后台管理界面的下拉框数据
     */
    @RequestMapping("/getjsonlist")
    public Result getjsonlist(){
        List<College>collegeList = collegeMapper.getCollege();
        JSONArray jsonArray = new JSONArray();
        JSONArray jsonArray1 = new JSONArray();
        List<Object>list = new ArrayList<>();
        collegeList.forEach(v->{
            JSONObject jsonObject = new JSONObject();
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("text",v.getCname());
            jsonObject2.put("value",v.getCname());
            jsonObject.put("label",v.getCname());
            jsonObject.put("value",v.getCname());
            jsonArray.put(jsonObject);
            jsonArray1.put(jsonObject2);
        });
        list.add(jsonArray);
        list.add(jsonArray1);
        return Result.success(200,"ok",list);
    }

    /**
     *
     * @return和上面的区别就是这个下拉返回的是id，而不是名称
     */
    @RequestMapping("/getjsonlistid")
    public Result getjsonlistid(){
        List<College>collegeList = collegeMapper.getCollege();
        JSONArray jsonArray = new JSONArray();
        JSONArray jsonArray1 = new JSONArray();
        List<Object>list = new ArrayList<>();
        collegeList.forEach(v->{
            JSONObject jsonObject = new JSONObject();
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("text",v.getCname());
            jsonObject2.put("value",v.getCid());
            jsonObject.put("label",v.getCname());
            jsonObject.put("value",v.getCid());
            jsonArray.put(jsonObject);
            jsonArray1.put(jsonObject2);
        });
        list.add(jsonArray);
        list.add(jsonArray1);
        return Result.success(200,"ok",list);
    }

    /**
     *
     * @return学院管理挂载的信息
     */
    @RequestMapping("/getClist")
    public Result getClist(){
        return Result.success(200,"ok",collegeMapper.getCollege());
    }

    /**
     *
     * @param name
     * @return 添加学院
     */
    @RequestMapping("addCollege")
    public Result addCollege(@RequestParam("collegename")String name){
        College college = new College();
        college.setCname(name);
        boolean flag = collegeService.save(college);
        if (flag){
            return Result.success(200,"ok",null);
        }else {
            return Result.fail(401,"添加失败",null);
        }

    }

    /**
     *
     * @param id
     * @return 删除学院
     */
    @RequestMapping("deleteCollege")
    public Result deleteCollege(@RequestParam("id")Integer id){
        boolean flag = collegeService.removeById(id);
        if (flag){
            return Result.success(200,"ok",null);
        }else {
            return Result.fail(401,"删除失败",null);
        }
    }

    /**
     *
     * @param id
     * @param cname
     * @return 修改学院数据
     */
    @RequestMapping("updateCollege")
    public Result updateCollege(@RequestParam("id")Integer id,@RequestParam("name")String cname){
        College college = collegeService.getOne(new QueryWrapper<College>().eq("cid",id));
        college.setCname(cname);
        boolean flag = collegeService.updateById(college);
        if (flag){
            return Result.success(200,"ok",null);
        }else {
            return Result.fail(401,"error",null);
        }
    }

    /**
     *
     * @param id
     * @return 检查是否可以删除
     */
    @RequestMapping("/CanDelete")
    public Result CanDelete(@RequestParam("id")Integer id){
        List<CkcDto>ckcDtoList = collegeMapper.candelete(id);
        if (ckcDtoList != null && ckcDtoList.size() > 0 && ckcDtoList.get(0) == null){
                return Result.success(200,"ok",true);
        }else {
            return Result.success(200, "ok", false);
        }
    }
    /**
     *
     * @param jsonArray
     * @param key
     * @param value
     * @return JSON数据处理方法
     */
    private JSONObject findObjectByKey(JSONArray jsonArray, String key, String value) {
        for (Object obj : jsonArray) {
            JSONObject jsonObj = (JSONObject) obj;
            if (jsonObj.getStr(key).equals(value)) {
                return jsonObj;
            }
        }
        return null;
    }

}
